import md5 from 'react-native-md5';

const MD5_UPPER = value => md5.hex_md5(`${value}`).toLocaleUpperCase();

const MD5_LOWER = value => md5.hex_md5(`${value}`).toLocaleLowerCase();

const versionCompare = (ver1, ver2) => {
  const version1pre = parseFloat(ver1);
  const version2pre = parseFloat(ver2);
  const version1next = ver1.replace(`${version1pre}.`, '');
  const version2next = ver2.replace(`${version2pre}.`, '');
  if (version1pre > version2pre) {
    return true;
  } if (version1pre < version2pre) {
    return false;
  }
  if (version1next >= version2next) {
    return true;
  }
  return false;
};

const parseNoThrow2Int = (value, radix) => {
  const rad = radix || 10;
  try {
    if (!value || value.length === 0) {
      return 0;
    }
    return parseInt(value, rad);
  } catch (error) {
    return 0;
  }
};

export default {
  MD5_UPPER,
  MD5_LOWER,
  versionCompare,
  parseNoThrow2Int,
};
